﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TaiwuZD
{
    public enum FeatureStarType
    {
        None,
        Attack,
        Defend,
        Wisdom
    }
    public enum FeatureStarColor
    {
        None,
        Blue,
        Red,
        White
    }

    public class FeatureStar
    {
        public FeatureStarType Type { get; set; }

        public FeatureStarColor Color { get; set; }
    }


    /// <summary>
    /// 角色特性
    /// </summary>
    public class RoleFeature
    {
        private static string[] Fileds = { "序号", "名称", "N", "N", "N", "N", "说明", "进攻", "防守", "机略", "合计", "类型", "程度", "N", "颜色", "N", "N", "健康上限", "内息缭乱", "细腻", "聪颖", "水性", "勇壮", "坚毅", "冷静", "福缘", "臂力", "体质", "灵敏", "根骨", "悟性", "定力", "生育率", "喜恶周期", "好感变化", "魅力", "护体", "御气", "内息", "卸力", "拆招", "闪避", "臂力比例", "体质比例", "灵敏比例", "根骨比例", "悟性比例", "定力比例", "护体比例", "御气比例", "内息比例", "卸力比例", "拆招比例", "闪避比例", "力道", "精妙", "N", "造成外伤", "造成内伤", "破体强度", "破气强度", "内功发挥", "移动速度", "N", "N", "提气速度", "施展速度", "攻击速度", "武器切换", "架势速度", "N", "外伤上限", "内伤上限", "N", "疗伤效率", "驱毒效率", "毒", "毒", "毒", "毒", "毒", "毒", "音律", "弈棋", "读书", "绘画", "术数", "品鉴", "锻造", "制木", "医术", "毒术", "织锦", "巧匠", "道法", "佛学", "厨艺", "杂学", "内功", "身法", "绝技", "掌法", "指法", "腿法", "暗器", "剑法", "刀法", "长兵", "奇门", "软兵", "御射", "乐器", };


        /// <summary>
        /// 原始数据
        /// </summary>
        private string RawContent { get; set; }


        private string[] FeatureValues { get; set; } //= new string[112];

        private int[] AvaliableFeatureValueId { get; set; }

        public string Name { get { return FeatureValues?.Length > 1 ? FeatureValues[1] : string.Empty; } }

        private int _Id;

        /// <summary>
        /// 序号
        /// </summary>
        public int Id
        {
            get
            {
                if (_Id == 0 && FeatureValues != null)
                {
                    int.TryParse(FeatureValues[0], out _Id);
                }
                return _Id;
            }
        }

        /// <summary>
        /// 说明
        /// </summary>
        public string Description { get { return FeatureValues?[6]; } }

        private string _FeaturesDisp;

        /// <summary>
        /// 特性效果
        /// </summary>
        public string FeaturesDisplay
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_FeaturesDisp))
                {
                    for (int i = 17; i < Fileds.Length; i++)
                    {
                        if (FeatureValues[i] != "0")
                        {
                            var val = (float)int.Parse(FeatureValues[i]) / 100;
                            var fid = Fileds[i];

                            if (fid.Contains("比例"))
                                _FeaturesDisp += $"{fid.Replace("比例","")}  :  {val:P0}\n";
                            else if (fid.Contains("毒"))
                                _FeaturesDisp += $"{fid}  :  {val:P2}\n";
                            else
                                _FeaturesDisp += $"{fid}  : {val:N2}\n";
                        }
                    }
                }

                return _FeaturesDisp;
            }
        }

        public FeatureStar[] FeatureStars { get; private set; } //= new FeatureStar[3];



        public static RoleFeature Load(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return null;

            var tmp = str.Split(',');
            if (tmp.Length != 112)
                throw new Exception("RoleFeature:加载的数据不匹配");

            RoleFeature rf = new RoleFeature();
            rf.RawContent = str;
            rf.FeatureValues = tmp;

            List<FeatureStar> lstars = new List<FeatureStar>();
            //特性star
            //进攻
            var cnt = Regex.Matches(tmp[7], "1").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Attack, Color = FeatureStarColor.Blue });
            }
            cnt = Regex.Matches(tmp[7], "2").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Attack, Color = FeatureStarColor.Red });
            }
            cnt = Regex.Matches(tmp[7], "3").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Attack, Color = FeatureStarColor.White });
            }
            //防守
            cnt = Regex.Matches(tmp[8], "1").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Defend, Color = FeatureStarColor.Blue });
            }
            cnt = Regex.Matches(tmp[8], "2").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Defend, Color = FeatureStarColor.Red });
            }
            cnt = Regex.Matches(tmp[8], "3").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Defend, Color = FeatureStarColor.White });
            }
            //机略
            cnt = Regex.Matches(tmp[9], "1").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Wisdom, Color = FeatureStarColor.Blue });
            }
            cnt = Regex.Matches(tmp[9], "2").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Wisdom, Color = FeatureStarColor.Red });
            }
            cnt = Regex.Matches(tmp[9], "3").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Wisdom, Color = FeatureStarColor.White });
            }

            //for (int i = 3 - lstars.Count; i >0; i--)
            //{
            //    lstars.Add(new FeatureStar());
            //}
            rf.FeatureStars = lstars.ToArray();



            List<int> list = new List<int>();
            for (int i = 10; i < 112; i++)
            {
                if (!string.IsNullOrWhiteSpace(tmp[i]) && tmp[i] != "0")
                {
                    list.Add(i);
                }
            }
            rf.AvaliableFeatureValueId = list.ToArray();
            return rf;
        }








    }

    public class ActorFeature 
    {
        public int Id { get; set; }

        public string Name { get; set; }

        /// <summary>
        /// 说明
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 效果
        /// </summary>
        /// <param name="id"></param>
        public string FeatureEffect { get; set; }

        /// <summary>
        /// 效果
        /// </summary>
        public Dictionary<int, int> Effects { get; set; } = new Dictionary<int, int>();

        /// <summary>
        /// 分类id
        /// </summary>
        public int CategoryId { get; set; }

        /// <summary>
        /// 在分类中的等级
        /// </summary>
        public int Degree { get; set; }

        public FeatureStar[] FeatureStars { get; private set; }


        internal static ActorFeature LoadFromJson(JProperty jp)
        {
            var af = new ActorFeature();

            var fv = jp.Value;

            af.Id = int.Parse(jp?.Name);
            af.Name = fv["0"].ToString();
            af.Description = fv["99"].ToString();
            af.CategoryId = fv["5"].ToObject<int>();
            af.Degree = fv["4"].ToObject<int>();

            #region 特性
            List<FeatureStar> lstars = new List<FeatureStar>();
            string attack = jp.Value["1"].ToString();
            string defend = jp.Value["2"].ToString();
            string smart = jp.Value["3"].ToString();
            //特性star
            //进攻
            var cnt = Regex.Matches(attack, "1").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Attack, Color = FeatureStarColor.Blue });
            }
            cnt = Regex.Matches(attack, "2").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Attack, Color = FeatureStarColor.Red });
            }
            cnt = Regex.Matches(attack, "3").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Attack, Color = FeatureStarColor.White });
            }
            //防守
            cnt = Regex.Matches(defend, "1").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Defend, Color = FeatureStarColor.Blue });
            }
            cnt = Regex.Matches(defend, "2").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Defend, Color = FeatureStarColor.Red });
            }
            cnt = Regex.Matches(defend, "3").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Defend, Color = FeatureStarColor.White });
            }
            //机略
            cnt = Regex.Matches(smart, "1").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Wisdom, Color = FeatureStarColor.Blue });
            }
            cnt = Regex.Matches(smart, "2").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Wisdom, Color = FeatureStarColor.Red });
            }
            cnt = Regex.Matches(smart, "3").Count;
            for (int i = 0; i < cnt; i++)
            {
                lstars.Add(new FeatureStar { Type = FeatureStarType.Wisdom, Color = FeatureStarColor.White });
            }

            af.FeatureStars = lstars.ToArray();
            #endregion

            //效果
            var jpp = jp.Value.Skip(16);
            foreach (JProperty item in jpp)
            {
                //var tmp = jp.Value.ElementAt(i) as JProperty;
                af.Effects.Add(int.Parse(item.Name), item.Value.ToObject<int>());
            }


            foreach (var item in af.Effects)
            {
                var FieldName = GameAssets.TwID[item.Key.ToString()];
                if (string.IsNullOrWhiteSpace(FieldName))
                    FieldName = $"未知项({item.Key})";
                var val =  item.Value ;

                if (FieldName.Contains("比例"))
                    af.FeatureEffect += $"{FieldName.Replace("比例", "")}  :  {(float)val/100:P0}\n";
                else if (FieldName.Contains("毒"))
                    af.FeatureEffect += $"{FieldName}  :  {(float)val / 100:P2}\n";
                else
                    af.FeatureEffect += $"{FieldName}  : {val:#,#.##}\n";
            }
             
            return af;
        }


    }








}
